﻿using Black.Minecraft.Editor.ViewModels.Core;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace Black.Minecraft.Editor.Views
{
    /// <summary>
    /// Vue principale de l'application
    /// </summary>
    public partial class MainView : Window
    {
        #region Variables d'instances

        private Point _startPoint;

        #endregion

        #region Constructeur

        /// <summary>
        /// Constructeur de la classe
        /// </summary>
        public MainView()
        {
            InitializeComponent();
        }

        #endregion

        #region Fonctions privées

        /// <summary>
        /// Recherche un ancêtre
        /// </summary>
        /// <typeparam name="T">Type de l'ancêtre</typeparam>
        /// <param name="current">Objet source</param>
        /// <returns>Instance de l'ancêtre, NULL sinon</returns>
        private T FindAnchestor<T>(DependencyObject current) where T : DependencyObject
        {
            do
            {
                if (current is T)
                    return (T)current;

                current = VisualTreeHelper.GetParent(current);
            }
            while (current != null);

            // Non trouvé

            return (null);
        }

        /// <summary>
        /// Se produit sur le click gauche du bouton de la souris
        /// </summary>
        /// <param name="sender">Source de l'appel</param>
        /// <param name="e">Argument de l'appel</param>
        private void OnItemsListPreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            this._startPoint = e.GetPosition(null);
        }

        /// <summary>
        /// Se produit sur le déplacement de la souris
        /// </summary>
        /// <param name="sender">Source de l'appel</param>
        /// <param name="e">Argument de l'appel</param>
        private void OnItemsListPreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            // Récupère la position courante de la sourie

            Point mousePos = e.GetPosition(null);
            Vector diff = this._startPoint - mousePos;

            if (e.LeftButton == MouseButtonState.Pressed && (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance || Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
            {
                // Récuple ListViewItem dragger

                ListBoxItem listViewItem = this.FindAnchestor<ListBoxItem>((DependencyObject)e.OriginalSource);

                if (listViewItem == null)
                    return;

                // Recherche la donnée de l'item

                BaseViewModel item = (BaseViewModel)((ListBox)sender).ItemContainerGenerator.ItemFromContainer(listViewItem);

                if (item == null)
                    return;

                // Initialise le processus de Drag & Drop

                DataObject dragData = new DataObject("ItemDataModel", item);
                DragDrop.DoDragDrop(listViewItem, dragData, DragDropEffects.Move | DragDropEffects.None);
            }
        }

        /// <summary>
        /// Se produit sur le click du bouton clear search
        /// </summary>
        /// <param name="sender">Source de l'appel</param>
        /// <param name="e">Argument de l'appel</param>
        private void OnClearSearchClick(object sender, RoutedEventArgs e)
        {
            this.SearchBox.Text = string.Empty;
        }

        #endregion
    }
}
